/*
 * @Descripttion:appStore
 * @LastEditors: xzh
 * @LastEditTime: 2024-04-21 17:52:27
 */

import { defineStore } from "pinia";
import { themeEnum } from "@/utils/constants/app/index.d";
import { app_Inf, navbar_Inf } from "./index.d";
import { object_Inf } from "@/utils/typings/index.type";

const useAppStore = defineStore("appStore", {
  state: (): app_Inf => ({
    navbar: {
      opened: true,
      theme: themeEnum.light,
      currentFactory: {}, //当前工厂
      factoryArr: [], //工厂数据源
    },
    language: {
      //当前语言
      current: "zh",
      //后缀
      suffix: "",
    },
    mobile: false,
    keepAliveInclude: [],
  }),
  //开启持久化
  persist: {
    enabled: true,
  },
  actions: {
    /**
     * @Author: xzh
     * @Descripttion: 设置导航栏
     * @Param:
     * @param {navbar_Inf} navbar
     */
    setNavbar(navbar: navbar_Inf) {
      this.navbar = navbar;
    },
    /**
     * @Author: xzh
     * @Descripttion: 设置缓存包含key
     * @Param:
     * @param {navbar_Inf} navbar
     */
    setKeepAliveInclude(keepAliveInclude: Array<string>) {
      this.keepAliveInclude = keepAliveInclude;
    },
    /**
     * @Author: xzh
     * @Descripttion: 设置是否手机模式
     * @Param:
     * @param {boolean} isMobile
     */
    setMobile(isMobile: boolean) {
      this.mobile = isMobile;
    },
    /**
     * @Author: xzh
     * @Descripttion: 设置当前工厂
     * @Param:
     * @param {object_Inf} currentFactory
     */
    setCurrentFactory(currentFactory: object_Inf) {
      this.navbar.currentFactory = currentFactory;
    },
    /**
     * @Author: xzh
     * @Descripttion: 设置工厂数据源
     * @Param:
     * @param {Array} factoryArr
     */
    setFactoryArr(factoryArr: Array<object_Inf>) {
      Object.assign(this.navbar, {
        factoryArr,
        currentFactory: factoryArr.find(() => true),
      });
    },
    /**
     * @Author: xzh
     * @Descripttion: 切换语言
     * @Param:
     * @param {string} language-语言
     */
    toggleLanguage(language: string) {
      Object.assign(this.language, {
        current: language,
        suffix: language == "zh" ? "" : "_" + language,
      });
    },
  },
});

export default useAppStore;
